<?php

session_start();

//header("Content-Type:application/vnd.adobe.pdf");

$fullname = $_SESSION["fullname"];
require_once('./fpdf/tcpdf/tcpdf.php');

//require_once('./fpdf/FPDI/fpdi.php');

class PDF extends TCPDF {

    function setVar($fullname) {
        $this->fullname = $fullname;
    }

    function Header() {
        $this->SetFont("thsarabun", "B", 14);
        $header = '
                            
            <table width="100%" border="0" cellspacing="0" cellpadding="0"><thead>
                <tr>
                  <td height="33" colspan="5">เทศบาลนครอุดรธานี (ผู้ปฏิบัติงาน : ' . $this->fullname . ')</td>
                  <td colspan="4" align="right">วันที่ ' . date('d / m / Y') . '</td>
                </tr>
                </thead>
                </table>
                <table width="100%" border="1" cellspacing="0" cellpadding="0"><thead>
                <tr align="center" nobr="true" valign="middle" >
                <td rowspan="2" width="8%" >วันที่ยืม</td>
                <td rowspan="2" width="7%" >เลขที่สัญญา</td>
                <td rowspan="2" width="13%">ชื่อผู้ยืม</td>
                <td rowspan="2" width="22%">รายการ</td>
                <td rowspan="2" width="8%">จำนวนเงิน</td>
                <td rowspan="2" width="8%">วันครบกำหนด</td>
                <td rowspan="2" width="8%">วันส่งใช้</td>
                <td colspan="2" width="18%">ส่งใช้เป็น</td>
                <td rowspan="2"width="8%">เอกสารเลขที่</td>
                </tr>
                 <tr align="center" nobr="true" style="border-bottom: 1px dotted black">
                <td>ใบสำคัญ</td>
                <td>เงินสด</td>               
            </tr>
               </thead></table><hr>';
        $this->writeHTML('<br> <div align="center">ทะเบียนเงินยืม</div>' . $header, true, false, false, false, '');
    }

}

//$pdf = new TCPDF($orientation = 'P', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);
$pdf = new PDF($orientation = 'L', $unit = 'mm', $format = 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);

$pdf->setVar($fullname);
$pdf->SetMargins(10, 38, 10);
$pdf->SetAutoPageBreak(true, 20);
$pdf->AddPage();

//$pdf->SetFont("thsarabunb", "", 14);
$pdf->SetFont('thsarabun', '', 14);

require_once("includes/connect.php");
require_once("includes/function.php");

$db = new myDBS();
$db->selectDB("account");

$sql = "SELECT  promise_date ,  card_id1 ,  reason ,  total ,  remain , tbl_pet_budget.pet_id, return_date"
        . ", remain_cash, return_amount, tbl_pet_budget.promise_id AS promise_id "
        . " FROM tbl_promise_budget "
        . " JOIN tbl_pet_budget ON tbl_promise_budget.id = tbl_pet_budget.promise_id "
        . " JOIN tbl_return ON tbl_promise_budget.id = tbl_return.promise_id "
        . " UNION"
        . " SELECT  promise_date ,  card_id AS card_id1 , objective AS reason , borrow_amount AS total "
        . " , remain , tbl_pet_borrow.pet_id, return_date"
        . ", remain_cash, return_amount, tbl_pet_borrow.borrow_id AS promise_id "
        . " FROM tbl_promise_borrow "
        . " JOIN tbl_pet_borrow ON tbl_promise_borrow.borrow_id = tbl_pet_borrow.borrow_id "
        . " JOIN tbl_return ON tbl_promise_borrow.borrow_id = tbl_return.borrow_id ";


//echo $sql;
$result = $db->runSQL($sql);

$tbl_header = '
<table cellspacing="0" cellpadding="0" border="0" width="100%">

';


while ($rows = $db->fetch_object($result)) {

    $tbl_body = $tbl_body . '<tr align="center">
                <td width="8%">' . $rows->promise_date . '</td>
                <td width="7%">' . $rows->promise_id . '</td>
                <td width="13%" align="left">' . get_name_person($rows->card_id1) . '</td>
                <td width="22%" align="left">' . $rows->reason . '</td>
                <td width="8%" align="right">' . number_format($rows->total, 2) . '</td>
                <td width="8%">&nbsp;</td>
                <td width="8%">' . $rows->return_date . '</td>
                <td width="9%" align="right">' . number_format($rows->return_amount, 2) . '</td>';

    if ($rows->remain_cash == "on") {
        $tbl_body .= '<td width="9%" align="right">' . number_format($rows->total - $rows->return_amount, 2) . '</td>';
    } else {
        $tbl_body .= '<td width="9%" align="right">&nbsp;</td>';
    }
    $tbl_body .= '<td width="8%">' .$rows->pet_id. '</td></tr>';
    
    $tbl_body .= '<tr height="1" nobr="true"><td colspan="10"><hr></td></tr>';
}

$tbl_footer = '</table>';

//$pdf->writeH
$pdf->writeHTML($tbl_header . $tbl_body . $tbl_footer, true, false, false, false, '');
//$pdf->writeHTML($sql);
//$pdf->Output();
//$pdf->writeHTML($tbl_header . $tbl . $tbl_sum, true, false, false, false, '');
$pdf->Output("pdf", 'I');
?>